#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace MZ
{
	template<class T>
	struct list_node
	{
		T _data;
		list_node<T>* _next;
		list_node<T>* _prev;
		list_node(const T& data = T())
			:_data(data)
			,_next(nullptr)
			,_prev(nullptr)
		{ }
	};
	template<class T>
	T& operator<<(ostream& out, list_node<T> Node)
	{
		out << Node._data << ' ';
		return out;
	}
	template<class T,class Ref, class Ptr>
	struct list_iterator
	{
		typedef list_node<T> Node;
		typedef list_iterator<T,Ref,Ptr> Self;
		Node* _node;
		list_iterator(Node* node)
			:_node(node)
		{}
		Ref operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			return &_node->_data;
		}
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		Self operator++(int)
		{
			Node tmp = _node;
			_node = _node->_next;
			return tmp;
		}
		Self& operator--()
		{
			_node = _node->prev;
			return *this;
		}
		Self operator--(int)
		{
			Node tmp = _node;
			_node = _node->_prev;
			return tmp;
		}
		bool operator!=(const Self& s) const
		{
			return _node != s._node;
		}
		bool operator==(const Self& s) const
		{
			return _node == s._node;
		}
	};
	/*template<class T>
	struct list_const_iterator
	{
		typedef list_node<T> Node;
		typedef list_const_iterator<T> Self;
		Node* _node;
		list_const_iterator(Node* node)
			:_node(node)
		{
		}
		T& operator*()
		{
			return _node->_data;
		}
		T* operator->()
		{
			return &_node->_data;
		}
		Self& operator++()
		{
			_node = _node->_next;
			return *this;
		}
		Self operator++(int)
		{
			Node tmp = _node;
			_node = _node->next;
			return tmp;
		}
		Self& operator--()
		{
			_node = _node->prev;
			return *this;
		}
		Self operator--(int)
		{
			Node tmp = _node;
			_node = _node->prev;
			return tmp;
		}
		bool operator!=(const Self& s) const
		{
			return _node != s._node;
		}
		bool operator==(const Self& s) const
		{
			return _node == s._node;
		}
	};*/
	template<class T>
	class list
	{
		typedef list_node<T> Node;
	public:
		typedef list_iterator<T, T&, T*> iterator;
		typedef list_iterator<T, const T&, const T*> const_iterator;
		iterator begin()
		{
			return _head->_next;
		}
		iterator end()
		{
			return _head;
		}
		const_iterator begin()const
		{
			return _head->_next;
		}
		const_iterator end()const
		{
			return _head;
		}
		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
			_size = 0;
		}
		list()
		{
			empty_init();
		}
		list(const list<T>& lt)
		{
			empty_init();
			for (auto& e : lt)
			{
				push_back(e);
			}
		}
		void clear()
		{
			list<T>::iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
		}
		void swap(list<T>& lt)
		{
			std::swap(_head, lt._head);
			std::swap(_size, lt._size);
		}
		list<T>& operator=(list<T> lt)
		{
			swap(lt);
			return *this;
		}
		void push_back(const T& x)
		{
		//	Node* tail = _head->_prev;
		//	Node* newnode = new Node(x);

		//	tail->_next = newnode;
		//	_head->_prev = newnode;
		//	newnode->_prev = tail;
		//	newnode->_next = _head;
		//
		//	_size++;
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		iterator insert(iterator pos, const T& x)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;

			Node* newnode = new Node(x);

			cur->_prev = newnode;
			newnode->_next = cur;
			newnode->_prev = prev;
			prev->_next = newnode;

			++_size;
			return newnode;
		}
		iterator erase(iterator pos)
		{
			assert(pos != end());
			Node* prev = pos._node->_prev;
			Node* next = pos._node->_next;
			
			prev->_next = next;
			next->_prev = prev;
			delete pos._node;
			--_size;
			return next;
		}
		void pop_back()
		{
			erase(--end());
		}
		void pop_front()
		{
			erase(begin());
		}
		size_t size() const
		{
			return _size;
		}
		bool empty()
		{
			return _size == 0;
		}
	private:
		Node* _head;
		size_t _size;
	};
	template<class Container>
	void print_container(const Container& lt)
	{
		typename Container::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << ' ';
			++it;
		}
		cout << endl;
	}
	struct AA
	{
		int _a1;
		int _a2;
		AA(int a1 = 1, int a2 = 2)
			:_a1(a1)
			, _a2(a2)
		{
		}
	};
	void test_list1()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
		lt.push_back(4);
		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << ' ';
			++it;
		}
		print_container(lt);
		/*cout << endl;
		for (auto e : lt)
		{
			cout << e << ' ';
		}
		cout << endl;*/
		//list<AA> LT;
		//LT.push_back(AA());
		//LT.push_back(AA());
		//LT.push_back(AA());
		//LT.push_back(AA());
		//list<AA>::iterator ita = LT.begin();
		//while (ita != LT.end())
		//{
		//	//cout << (*ita)._a1 << ' ' << (*ita)._a2<<' ';
		//	cout << ita->_a1 << ' ' << ita->_a2 << ' ';
		//	++ita;
		//}
	}
	void test_list2()
	{
		list<int> lt1;
		lt1.push_back(10);
		lt1.push_back(20);
		lt1.push_back(30);
		lt1.push_back(40);

		list<int> lt2(lt1);
		print_container(lt1);
		print_container(lt2);
   		list<int> lt3;
		lt3 = lt2;
		print_container(lt3);
	}
}